package com.cch.cooperation.api.bookkeepping.controller;

import com.cch.cooperation.api.auth.dto.LoginUserDTO;
import com.cch.cooperation.api.bookkeepping.dto.*;
import com.cch.cooperation.api.bookkeepping.service.BookkeepingServiceImpl;
import com.cch.cooperation.api.common.request.IdReq;
import com.cch.cooperation.biz.common.page.PageReq;
import com.cch.cooperation.common.web.response.Response;
import com.cch.cooperation.common.web.session.Session;
import com.cch.cooperation.common.web.session.SessionManager;
import com.cch.cooperation.dao.bookkeeping.dto.BookkeepingQueryDTO;
import com.cch.cooperation.dao.bookkeeping.model.Bookkeeping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 收支明细接口
 *
 * @author cch
 */
@Api(tags = "收支明细接口")
@RestController
@RequestMapping("/api/bookkeeping")
public class BookkeepingController {

    @Resource
    private BookkeepingServiceImpl bookkeepingService;
    @Resource
    private SessionManager sessionManager;

    @ApiOperation("查询列表")
    @GetMapping("/queryList")
    public Response<BookkeepingPageListDTO> queryList(BookkeepingQueryDTO query, PageReq pageReq) {
        BookkeepingPageListDTO data = bookkeepingService.queryList(query, pageReq);
        return Response.success(data);
    }

    @ApiOperation("根据描述添加")
    @PostMapping("/addByDescription")
    public Response<List<BookkeepingDTO>> addByDescription(@RequestBody BookkeepingDescriptionAddDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        req.setUserId(session.getData().getId());
        List<BookkeepingDTO> data = bookkeepingService.addByDescription(req);
        return Response.success(data);
    }

    @ApiOperation("解析描述")
    @PostMapping("/parseDescription")
    public Response<List<Bookkeeping>> parseDescription(@RequestBody BookkeepingDescriptionParseDTO req) {
        List<Bookkeeping> data = bookkeepingService.inferByDescription(req.getDescription());
        return Response.success(data);
    }

    @ApiOperation("添加")
    @PostMapping("/add")
    public Response<BookkeepingDTO> add(@RequestBody BookkeepingAddDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        req.setUserId(session.getData().getId());
        BookkeepingDTO data = bookkeepingService.add(req);
        return Response.success(data);
    }

    @ApiOperation("根据id修改")
    @PostMapping("/updateById")
    public Response<BookkeepingDTO> updateById(@RequestBody Bookkeeping bookkeeping) {
        BookkeepingDTO data = bookkeepingService.updateById(bookkeeping);
        return Response.success(data);
    }

    @ApiOperation("根据id删除")
    @PostMapping("/deleteById")
    public Response<Void> deleteById(@RequestBody IdReq req) {
        bookkeepingService.deleteById(req.getId());
        return Response.success();
    }

}
